package cn.chen.controller;

import cn.chen.model.User;
import cn.chen.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hbpu
 * @since 2023-05-28
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //增
    @PostMapping("/add")
    public boolean addUser(@RequestBody User user) {
        return userService.save(user);
    }

    //删
    @GetMapping("/delete")
    public boolean deleteUser(User user) {
        return userService.removeById(user);
    }

    //改
    @PostMapping("/update")
    public boolean updateUser(@RequestBody User user) {
        return userService.updateById(user);
    }

    //查
    @GetMapping("/get")
    public User getUser(Integer id) {
        return userService.getById(id);
    }

    //登录
    @GetMapping("/login")
    public User login(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        wrapper.eq("password", user.getPassword());
        return userService.getOne(wrapper);
    }

    //根据id查询子用户们
    @GetMapping("/getUsersByUid")
    public List<User> getUsersByDid(@RequestParam Integer uid) {
        //根据uid查询用户
        User user = userService.getById(uid);
        //获取部门id
        Integer did = user.getDid();

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        //比did小的都是子部门
        wrapper.lt(User::getDid, did);
        return userService.list(wrapper);
    }


}
